package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.beans.beancontext.BeanContext;
import java.util.List;

@RequestMapping("/user/shoppingCart")
@RestController
@Slf4j
@Api("c端购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加购物车
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addShopping(@RequestBody ShoppingCartDTO shoppingCartDTO)
    {
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list()
    {
        //获取当前用户id
        List<ShoppingCart> cartList = shoppingCartService.getShoppingCart();
        return Result.success(cartList);
    }

    //清空购物车
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean()
    {
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    //删除购物车中一个商品
    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO)
    {
        shoppingCartService.sub(shoppingCartDTO);
        return Result.success();
    }
}
